﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;
using System.Collections.Generic;
using Microsoft.SharePoint.Utilities;

namespace TestWorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWorkflow
{
    public sealed partial class WorkflowTaskReplicatorWF : StateMachineWorkflowActivity
    {
        public WorkflowTaskReplicatorWF()
        {
            InitializeComponent();
        }

        public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties();
        public static DependencyProperty createTaskWithContentType1_ContentTypeId1Property = DependencyProperty.Register("createTaskWithContentType1_ContentTypeId1", typeof(System.String), typeof(TestWorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWF));

        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [CategoryAttribute("Misc")]
        public String createTaskWithContentType1_ContentTypeId1
        {
            get
            {
                return ((string)(base.GetValue(TestWorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWF.createTaskWithContentType1_ContentTypeId1Property)));
            }
            set
            {
                base.SetValue(TestWorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWF.createTaskWithContentType1_ContentTypeId1Property, value);
            }
        }

        public static DependencyProperty createTaskWithContentType1_TaskId1Property = DependencyProperty.Register("createTaskWithContentType1_TaskId1", typeof(System.Guid), typeof(TestWorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWF));

        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [CategoryAttribute("Misc")]
        public Guid createTaskWithContentType1_TaskId1
        {
            get
            {
                return ((System.Guid)(base.GetValue(TestWorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWF.createTaskWithContentType1_TaskId1Property)));
            }
            set
            {
                base.SetValue(TestWorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWF.createTaskWithContentType1_TaskId1Property, value);
            }
        }

        public static DependencyProperty createTaskWithContentType1_TaskProperties1Property = DependencyProperty.Register("createTaskWithContentType1_TaskProperties1", typeof(Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties), typeof(TestWorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWF));

        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [CategoryAttribute("Misc")]
        public SPWorkflowTaskProperties createTaskWithContentType1_TaskProperties1
        {
            get
            {
                return ((Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties)(base.GetValue(TestWorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWF.createTaskWithContentType1_TaskProperties1Property)));
            }
            set
            {
                base.SetValue(TestWorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWF.createTaskWithContentType1_TaskProperties1Property, value);
            }
        }

        private void createTaskWithContentType1_MethodInvoking(object sender, EventArgs e)
        {
            //Init tasks
            createTaskWithContentType1_TaskProperties1 = new SPWorkflowTaskProperties();
            createTaskWithContentType1_TaskId1 = Guid.NewGuid();
            createTaskWithContentType1_ContentTypeId1 = WorkflowTaskReplicator.Helper.WorkflowTaskReplicatorContentTypeId.ToString();

            //Init users
            SPUser userAdmin = workflowProperties.Web.EnsureUser("administrator");
            SPUser userTest01 = workflowProperties.Web.EnsureUser("test01");
            SPUser userTest02 = workflowProperties.Web.EnsureUser("test02");

            //Make sure the task replicator content type is installed
            if (!WorkflowTaskReplicator.Helper.IsWorkflowTaskReplicatorCTAttachedToList(workflowProperties.TaskList))
                WorkflowTaskReplicator.Helper.AttachWorkflowTaskReplicatorCTOnList(workflowProperties.TaskList);

            //Set task metadata
            WorkflowTaskReplicator.Helper.SetTaskExtendedProperties(createTaskWithContentType1_TaskProperties1.ExtendedProperties,
                true, //All sub tasks must be completed
                new SPContentTypeId("0x0108"), //Sub task content type id
                new List<SPUser>() { userAdmin, userTest01, userTest02 }, //Assign to users
                new List<SPUser>() { userAdmin }, //Additional contributors
                new List<SPUser>() { userTest01 }, //Additional owners
                new List<SPUser>() { userTest02 }, //Additional readers
                new Dictionary<string, string> { { "Title", "Halloha!!" }, //Sub task meta data
                                                 { "DueDate", SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now.AddDays(1)) } });
        }

        public static DependencyProperty onTaskChanged1_AfterProperties1Property = DependencyProperty.Register("onTaskChanged1_AfterProperties1", typeof(Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties), typeof(TestWorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWF));

        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [CategoryAttribute("Misc")]
        public SPWorkflowTaskProperties onTaskChanged1_AfterProperties1
        {
            get
            {
                return ((Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties)(base.GetValue(TestWorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWF.onTaskChanged1_AfterProperties1Property)));
            }
            set
            {
                base.SetValue(TestWorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWF.onTaskChanged1_AfterProperties1Property, value);
            }
        }

        public static DependencyProperty onTaskChanged1_BeforeProperties1Property = DependencyProperty.Register("onTaskChanged1_BeforeProperties1", typeof(Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties), typeof(TestWorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWF));

        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [CategoryAttribute("Misc")]
        public SPWorkflowTaskProperties onTaskChanged1_BeforeProperties1
        {
            get
            {
                return ((Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties)(base.GetValue(TestWorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWF.onTaskChanged1_BeforeProperties1Property)));
            }
            set
            {
                base.SetValue(TestWorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWorkflow.WorkflowTaskReplicatorWF.onTaskChanged1_BeforeProperties1Property, value);
            }
        }

        bool isFinished;

        private void onTaskChanged1_Invoked(object sender, ExternalDataEventArgs e)
        {
            isFinished = onTaskChanged1_AfterProperties1.PercentComplete == 1F;
        }

        private void TaskCompleteCheck(object sender, ConditionalEventArgs e)
        {
            e.Result = isFinished;
        }

        private void codeActivity1_ExecuteCode(object sender, EventArgs e)
        {
            //Get sub task id's and check if they have been completed
            SPListItem taskItem = workflowProperties.TaskList.GetItemById(onTaskChanged1_AfterProperties1.TaskItemId);

            Dictionary<Guid, bool> results = WorkflowTaskReplicator.Helper.GetSubTasksInfo(taskItem);
        }
    }
}
